package com.lineage.server.model.Instance.npcai;

import java.util.HashSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Square {
  private static final Log _log = LogFactory.getLog(Square.class);
  
  private final int _x;
  
  private final int _y;
  
  private final CheckPath _maze;
  
  private final HashSet<Square> _adjacencies;
  
  private Square _parent;
  
  private boolean _start;
  
  private boolean _end;
  
  private boolean _open = false;
  
  public Square(int x, int y, CheckPath maze) {
    this._x = x;
    this._y = y;
    this._maze = maze;
    this._adjacencies = new HashSet<>();
  }
  
  public int getX() {
    return this._x;
  }
  
  public int getY() {
    return this._y;
  }
  
  public boolean isStart() {
    return this._start;
  }
  
  public void setStart(boolean start) {
    this._start = start;
  }
  
  public boolean isEnd() {
    return this._end;
  }
  
  public void setEnd(boolean end) {
    this._end = end;
  }
  
  public HashSet<Square> getAdjacencies() {
    return this._adjacencies;
  }
  
  public Square getParent() {
    return this._parent;
  }
  
  public void setParent(Square parent) {
    this._parent = parent;
  }
  
  public void calculateAdjacencies() {
    try {
      int bottom = this._x + 1;
      int right = this._y + 1;
      if (bottom < this._maze.getRows() && this._maze.getSquare(bottom, this._y).is_open()) {
        this._maze.getSquare(bottom, this._y).addAdjacency(this);
        addAdjacency(this._maze.getSquare(bottom, this._y));
      } 
      if (right < this._maze.getColumns() && this._maze.getSquare(this._x, right).is_open()) {
        this._maze.getSquare(this._x, right).addAdjacency(this);
        addAdjacency(this._maze.getSquare(this._x, right));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void addAdjacency(Square square) {
    this._adjacencies.add(square);
  }
  
  public void removeAdjacency(Square square) {
    this._adjacencies.remove(square);
  }
  
  public boolean is_open() {
    return this._open;
  }
  
  public void set_open(boolean open) {
    this._open = open;
  }
}
